﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace PMS.Logic.BD
{
    public partial class BottleSupplierAjax : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            String methodName = Request["method"];
            Type type = this.GetType();
            MethodInfo method = type.GetMethod(methodName);
            if (method == null) throw new Exception("method is null");
            try
            {

                method.Invoke(this, null);
            }
            catch
            {
                throw;
            }
        }

        /// <summary>
        /// 获取气瓶信息
        /// </summary>
        public void GetBottleSupplierList()
        {
            List<SearchCondition> listSearch = new List<SearchCondition>();
            if (Request.Form["searchData"] != null)
            {
                string searchData = Request.Form["searchData"];
                listSearch = ModelTools.GetSearchContions(searchData);
            }
            Expression<Func<BD_BottleSupplier, bool>> exp = SearchExpression.FindByGroup<BD_BottleSupplier>(listSearch);
            string OrderId = OperateContext.Sort == null ? "ID" : OperateContext.Sort;
            int count = 0;
            var list = DALSession.Current.BD_BottleSupplierDAL.GetPagedList(OperateContext.Page, OperateContext.Rows, exp, OrderId, OperateContext.Order);
            count = DALSession.Current.BD_BottleSupplierDAL.GetCount(exp);
            Response.Write(JsonHelper.toPageJson(list, count));
        }

        /// <summary>
        /// 根据气瓶厂家信息
        /// </summary>
        public void GetBottleSupplierName()
        {
            Response.Write(JsonHelper.toJson(DALSession.Current.BD_BottleSupplierDAL.GetListBy().Select(p => new { value = p.ID, text = p.BottleSupplier })));
        }

        /// <summary>
        /// 根据q获取气瓶厂家信息
        /// </summary>
        public void GetBottleSupplierByQ()
        {
            string q = Request.Form["q"];
            var a = new List<BD_BottleSupplier>();
            List<SearchCondition> listSearch = new List<SearchCondition>();
            Expression<Func<BD_BottleSupplier, bool>> exp = SearchExpression.FindByGroup<BD_BottleSupplier>(listSearch);
            string OrderId = OperateContext.Sort == null ? "ID" : OperateContext.Sort;
            int count = 0;
            if (q == null)
            {
                var list = DALSession.Current.BD_BottleSupplierDAL.GetPagedList(OperateContext.Page, OperateContext.Rows, exp, OrderId, false);
                count = DALSession.Current.BD_BottleSupplierDAL.GetCount(exp);
                Response.Write(JsonHelper.toPageJson(list, count));
            }
            else
            {
                SearchCondition search = new SearchCondition();
                search.Field = "MadeCode";
                search.Value = q;
                search.Operator = "co";
                listSearch.Add(search);
                exp = SearchExpression.FindByGroup<BD_BottleSupplier>(listSearch);
                var list = DALSession.Current.BD_BottleSupplierDAL.GetPagedList(OperateContext.Page, OperateContext.Rows, exp, OrderId, false);
                count = DALSession.Current.BD_BottleSupplierDAL.GetCount(exp);
                Response.Write(JsonHelper.toPageJson(list, count));
            }
        }

        /// <summary>
        /// 修改气瓶信息
        /// </summary>
        public void EditBottleSupplier()
        {
            int ID = Commons.String2Int32(Request.Form["ID"]);
            BD_BottleSupplier perNew = DALSession.Current.BD_BottleSupplierDAL.GetListBy(d => d.ID == ID).FirstOrDefault();
            ModelTools.Form2EditModel(Request.Form, perNew);

            perNew.FillDate = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            perNew.FillPerson = OperateContext.SessBasicInfo.UserInfo.Name;
            DALSession.Current.BD_BottleSupplierDAL.ModifyAllModel(perNew);
            DALSession.Current.BD_BottleSupplierDAL.SaveChanges();
            Response.Write(JsonHelper.toDefaultMsg(true, "保存成功"));
        }

        /// <summary>
        /// 添加气瓶信息
        /// </summary>
        public void AddBottleSupplier()
        {
            BD_BottleSupplier per = new BD_BottleSupplier();
            ModelTools.Form2EditModel(Request.Form, per);
            per.FillDate = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            per.FillPerson = OperateContext.SessBasicInfo.UserInfo.Name;

            DALSession.Current.BD_BottleSupplierDAL.Add(per);
            DALSession.Current.BD_BottleSupplierDAL.SaveChanges();
            Response.Write(JsonHelper.toDefaultMsg(true, "保存成功"));
        }

        /// <summary>
        /// 删除气瓶信息
        /// </summary>
        public void DelBottleSupplier()
        {
            //  int Id = Commons.String2Int32(Request.Form["Id"]);
            if (Request.Form["ID"] == null)
            {
                Response.Write(JsonHelper.toDefaultMsg(false, "删除失败"));
                return;
            }
            int Id = Convert.ToInt32(Request.Form["ID"].ToString());
            DALSession.Current.BD_BottleSupplierDAL.DelBy(p => p.ID == Id);
            DALSession.Current.BD_BottleSupplierDAL.SaveChanges();
            Response.Write(JsonHelper.toDefaultMsg(true, "删除成功"));
        }

    }
}